package com.example.demo.controller;

import com.example.demo.bean.Goods;
import com.example.demo.bean.User;
import com.example.demo.bean.view.GoodsView;
import com.example.demo.bean.view.OrderView;
import com.example.demo.bean.view.Result;
import com.example.demo.service.GoodsService;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class GoodsController {
    @Autowired(required = true)
    @Qualifier("GoodsService")
    private GoodsService goodsService;
    @RequestMapping("/insertGoods")
    @ResponseBody
    public Result<?> insertGoods()
    {
        Goods goods = new Goods();
        System.out.println(System.currentTimeMillis());
        String id = System.currentTimeMillis() + "";
        System.out.println(id);
        goods.setGoodsNum(id);
        goods.setSellerNum("123");
        goods.setGoodsName("fs");
        goods.setImagUrl("da");
        goods.setGoodsIntroduction("introduction");
        goods.setGoodsType("1");
        goods.setPrice(12);

        if(goodsService.insertGoods(goods))
            return Result.success();
        else
            return Result.error("454", "添加商品失败");
    }
    @RequestMapping("/deleteGoodsById")
    @ResponseBody
    public Result<?> deleteGoodsById(){
        Goods goods = new Goods();
        goods.setGoodsNum("goodsNum1");
        goods.setSellerNum("123");
        goods.setGoodsName("fs");
        goods.setImagUrl("da");
        goods.setGoodsIntroduction("introduction");
        goods.setGoodsType("1");
        goods.setPrice(12);
        if (goodsService.deleteGoods(goods))
            return Result.success();
        else
            return Result.error("345", "删除商品失败");
    }

    @RequestMapping("/updateGoodsById")
    @ResponseBody
    public Result<?> updateGoodsById(){
        Goods goods = new Goods();
        Goods preGoods = new Goods();
        preGoods.setGoodsNum("test");
        goods.setGoodsNum("goodsNum1");
        goods.setSellerNum("123");
        goods.setGoodsName("fs");
        goods.setImagUrl("da");
        goods.setGoodsIntroduction("introduction");
        goods.setGoodsType("1");
        goods.setPrice(12);

        if(goodsService.updateGoods(preGoods, goods))
            return Result.success();
        else
            return Result.error("454", "更新商品数据失败");
    }

    @RequestMapping("/findAllGoods")
    @ResponseBody
    public Result<?> findAllGoods(){
        PageHelper.startPage(1,1);//应设为变量
        List<Goods> goodsList = goodsService.findAllGoods();
        if(goodsList != null) {
            PageInfo<Goods> info = new PageInfo<>(goodsList);
            return Result.success(info);
        }
        else
            return Result.error("344", "查询订单失败");
    }

    @RequestMapping("/findGoodsByOwner")
    @ResponseBody
    public Result<?> findGoodsByOwner(){

        User user = new User();
        user.setUserNum("test");


        List<GoodsView> goodsViewList = goodsService.findGoodsByOwner(user);
        if(goodsViewList != null) {
            PageHelper.startPage(1,1);//应设为变量
            PageInfo<GoodsView> info = new PageInfo<>(goodsViewList);
            return Result.success(info);
        }
        else
            return Result.error("344", "查询商品失败");

    }
    @RequestMapping("/findGoodsById")
    @ResponseBody
    public Result<?> findGoodsById(){

        GoodsView goodsView = new GoodsView();
        goodsView.setGoodsNum("test");
        goodsView = goodsService.findGoodsById(goodsView.getGoodsNum());
        if(goodsView != null)
            return Result.success(goodsView);
        else
            return null;
    }

    @RequestMapping("/findGoodsByName")
    @ResponseBody
    public Result<?> findGoodsByName()
    {

        Goods goods = new Goods();
        goods.setGoodsName("上衣");
        List<GoodsView> goodsViewList = goodsService.findGoodsByName(goods.getGoodsName());
        if(goodsViewList != null)
        {
            PageHelper.startPage(1,1);
            PageInfo<GoodsView> info = new PageInfo<>(goodsViewList);
            return Result.success(info);
        }
        else
        {
            return Result.error("564", "搜索商品信息失败");
        }
    }
    @RequestMapping("/findAllGoodsView")
    @ResponseBody
    public Result<?> findAllGoodsView()
    {

        List<GoodsView> goodsViewList = goodsService.findAllGoodsView();
        if(goodsViewList != null) {
            PageHelper.startPage(1,1);
            PageInfo<GoodsView> info = new PageInfo<>(goodsViewList);
            return Result.success(info);
        }
        else
            return Result.error("344", "管理员查询所有商品失败");
    }
}
